package com.weiqiao.mall.service.impl;

import com.google.gson.Gson;
import com.weiqiao.mall.domain.SupplierEntity;
import com.weiqiao.mall.repository.ISupplierRepository;
import com.weiqiao.mall.service.ISupplierService;
import com.weiqiao.utils.StringUtils;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kjy
 * @createDate 2019-10-31
 */
@Service("SupplierServiceImpl")
public class SupplierServiceImpl implements ISupplierService {

    @Autowired
    @Qualifier("SupplierRepositoryImpl")
    private ISupplierRepository supplierRepository;

    @Override
    public List<SupplierEntity> findAllSupplier(int company_id, Bson filter, Bson sort) {
        List<String> listJson = supplierRepository.findAllSupplier(company_id, filter, sort);
        List<SupplierEntity> list = new ArrayList<>();
        if (listJson != null && listJson.size() > 0) {
            for (int i = 0; i < listJson.size(); i++) {
                list.add(new Gson().fromJson(listJson.get(i), SupplierEntity.class));
            }
        }
        return list;
    }

    @Override
    public SupplierEntity findSupplierInfo(int company_id, Bson filter) {
        SupplierEntity entity = new SupplierEntity();
        String info = supplierRepository.findSuppler(company_id, filter);
        if (StringUtils.isNotBlank(info)) {
            entity = new Gson().fromJson(info, SupplierEntity.class);
        }
        return entity;
    }
}
